package com.gopay.checkorder.service.task.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gopay.checkorder.service.CheckGopayOrderService;
import com.gopay.common.constants.order.CheckOrderJoin;
import com.gopay.common.domain.cps.CpsOrderAdjustRecord;
import com.gopay.common.exception.GopayException;

@Service("checkGopayOrderTaskThread")
public class CheckGopayOrderTaskThread implements Runnable {
    @Resource(name = "checkGopayOrderService")
    private CheckGopayOrderService checkGopayOrderService;

    private LinkedBlockingQueue<CpsOrderAdjustRecord> queue;
    private CountDownLatch latch;

    @Override
    public void run() {
        CpsOrderAdjustRecord record = queue.poll();

        if (record != null) {
            // 对单
            try {
                checkGopayOrderService.checkOrder(record, CheckOrderJoin.SYSTEM);
            } catch (GopayException e) {
                e.printStackTrace();
            }
        }

        latch.countDown();
    }

    public void setQueue(LinkedBlockingQueue<CpsOrderAdjustRecord> queue) {
        this.queue = queue;
    }

    public void setLatch(CountDownLatch latch) {
        this.latch = latch;
    }

}
